package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.CountsVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class UserLikeImpl implements UserLikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 保存或者更新
     * @param userId  当前用户id
     * @param likeUserId 喜欢的用户id
     * @param isLike 是否喜欢
     * @return
     */
    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1.构造条件，查询是否已经喜欢
            Criteria criteria = Criteria.where("userId").is(userId)
                    .and("likeUserId").is(likeUserId);
            Query query = Query.query(criteria);
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2.判断是否存在
            if (userLike == null) {
                //不存在就保存
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            }
            //3.存在就更新
            Update update = Update.update("isLike", isLike)
                    .set("updated", System.currentTimeMillis());
            mongoTemplate.updateFirst(query,update, UserLike.class);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            //抛异常就返回false
            return false;
        }
    }

    /**
     * 统计互相喜欢，喜欢，粉丝的数量
     * @param userId
     * @return
     */
    @Override
    public CountsVo counts(Long userId) {
        CountsVo vo = new CountsVo();
        //1.构建条件查询粉丝的数量 喜欢的用户id是自己的数量
        Query query1 = Query.query(Criteria.where("likeUserId").is(userId)
                .and("isLike").is(true) );
        List<UserLike> likeList = mongoTemplate.find(query1, UserLike.class);
        //获取所有喜欢我的用户id
        List<Long> values = CollUtil.getFieldValues(likeList, "userId", Long.class);
        //把idvo中
        vo.setFanCounts(values);
        vo.setFanCount(likeList.size());
        //2.构建条件查询喜欢的数量，用户id为自己的数量
        Query query2 = Query.query(Criteria.where("userId").is(userId)
                .and("isLike").is(true));
        List<UserLike> likes = mongoTemplate.find(query2, UserLike.class);
        //获取所有我喜欢的用户id
        List<Long> loves = CollUtil.getFieldValues(likes, "likeUserId", Long.class);
        //把我喜欢的用户id存到vo中
        vo.setLoveCounts(loves);
        vo.setLoveCount(likes.size());
        return vo;
    }

    /**
     * 查询喜欢的人
     * @param userId
     * @return
     */
    @Override
    public List<UserLike> finds(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("isLike").is(true));
        List<UserLike> likes = mongoTemplate.find(query, UserLike.class);
        return likes;
    }
}
